/*
 * PizzaManagerStandalone.java
 *
 * Created on 2 novembre 2007, 14:43
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ifips5.nal.epizza.web;

import com.ifips5.nal.epizza.util.JDBCManager;
import com.ifips5.nal.epizza.web.model.Pizza;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.sql.RowSet;
import javax.sql.rowset.WebRowSet;

/**
 *
 * @author Moi
 */
public class PizzaManagerStandalone
{
    
    
    private BeanManager beanManager = null;
    private Map<Integer, Pizza> pizzaMap = new HashMap();
    private List<Pizza> pizzaList = new LinkedList();
    private Pizza currentPizza = null;
    
    
    public PizzaManagerStandalone(BeanManager beanManager)
    {
        this.beanManager = beanManager;
    }
    
    
    /** Creates a new instance of PizzaManagerStandalone */
//    public PizzaManagerStandalone()
//    {
//    }
    
  
    
    
    
    
    public void getAllPizzas()
    {
        // Chargement des ingredients
        //IngredientManager ingredientManager = new IngredientManager();
        
        
        beanManager.getIngredientManager().loadIngredients();
        
        //ingredientManager.loadIngredients();
        
        
        
        
        String command =    "SELECT * FROM pizza, ingredient_has_pizza " +
                            "WHERE pizza.idPizza = ingredient_has_pizza.Pizza_idPizza " +
                            "ORDER BY pizza.idPizza";
        
        WebRowSet wrs = JDBCManager.getInstance().getRowSet(command, null);
        try
        {
            int currentIdPizza = -1;
            Pizza currentPizza = null;
            
            while(wrs.next())
            {
                if(wrs.getInt("idPizza") != currentIdPizza)
                {
                    currentPizza = new Pizza(wrs.getInt(1), wrs.getString(2), wrs.getBoolean(3), wrs.getBoolean(4), wrs.getDouble(5));
                    // On ajoute la pizza a la liste
                    this.pizzaList.add(currentPizza);
                    this.pizzaMap.put(wrs.getInt(1), currentPizza);
                    currentIdPizza = wrs.getInt(1);
                }
                //System.out.println(wrs.getInt("Ingredient_idIngredient"));
                //currentPizza.addIngredient(ingredientManager.getIngredientMap().get(wrs.getInt("Ingredient_idIngredient")));
                currentPizza.addIngredient(beanManager.getIngredientManager().getIngredientMap().get(wrs.getInt("Ingredient_idIngredient")));
            }
            //wrs.close();
        } catch (SQLException ex)
        {
            System.out.println("PB SQL PIZZA");
            ex.printStackTrace();
        }
    }
    
    
    
    
    
    public void printRowSet(RowSet rs)
    {
        try
        {
            ResultSetMetaData rsmd = rs.getMetaData();
            int nbColumn = rsmd.getColumnCount();
            for(int i = 1; i <= nbColumn; i++ )
            {
                System.out.print(rsmd.getColumnName(i) + "\t\t");
            }
            System.out.println("");
            
            while(rs.next())
            {
                System.out.print("-");
                for(int i = 1; i<= nbColumn; i++)
                {
                    System.out.print(rs.getObject(i)+"\t\t");
                }
                System.out.println("");
            }
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }
    
    
    
    //////////////////////
    // Getters and Setters

    

    public Map<Integer, Pizza> getPizzaMap()
    {
        return pizzaMap;
    }

    public void setPizzaMap(Map<Integer, Pizza> pizzaMap)
    {
        this.pizzaMap = pizzaMap;
    }

    public List<Pizza> getPizzaList()
    {
        return pizzaList;
    }

    public void setPizzaList(List<Pizza> pizzaList)
    {
        this.pizzaList = pizzaList;
    }

    public Pizza getCurrentPizza()
    {
        return currentPizza;
    }

    public void setCurrentPizza(Pizza currentPizza)
    {
        this.currentPizza = currentPizza;
    }    

    public BeanManager getBeanManager()
    {
        return beanManager;
    }

    public void setBeanManager(BeanManager beanManager)
    {
        this.beanManager = beanManager;
    }

}
